package com.stylefeng.guns.modular.apple.utils;


import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * @Description 时间相关工具类
 * @Author Mr.God
 * @Date 2020-03-16 14:21
 **/
public class TimeUtil {
    /**
     * date砖字符串
     * @param date
     * @param format 转换格式
     * @return
     */
    public static String dateToString(Date date,String format){
        return getGMTDateFormat(format).format(date);
    }

    public static SimpleDateFormat getGMTDateFormat(String format){
        SimpleDateFormat dateFormatterChina = new SimpleDateFormat(format);
        TimeZone timeZoneChina = TimeZone.getTimeZone("GMT+8");//获取时区
        dateFormatterChina.setTimeZone(timeZoneChina);//设置系统时区
        return dateFormatterChina;
    }

    public static String dateToGTMString(Date date,String format){
        if (date != null) {
            return getGMTDateFormat(format).format(date);
        }else{
            return "";
        }
    }

    public static String formatDate(Date date, String pattern) {
        String formatDate = null;
        if (date == null){
            return "";
        }
        if (StringUtils.isNotBlank(pattern)) {
            formatDate = DateFormatUtils.format(date, pattern);
        } else {
            formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
        }
        return formatDate;
    }
}
